package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.MemberReadHistory;
import com.genesis.mall.mapper.MemberReadHistoryMapper;
import com.genesis.mall.service.IMemberReadHistoryService;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: HShuai
 * @Date: 2022/08/03/21:57星期三
 * @Description:
 */

@Service
public class MemberReadHistoryServiceImpl extends ServiceImpl<MemberReadHistoryMapper, MemberReadHistory>
        implements IMemberReadHistoryService {

    @Override
    public void clearById(HttpServletRequest request) {
        //获取用户id
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(6);
        Claims claims = TokenUtil.getClaims(token);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());

        LambdaQueryWrapper<MemberReadHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberReadHistory::getMemberId, memberId);
        remove(lqw);


    }

    @Override
    public void deleteById(Long ids, HttpServletRequest request) {
        //获取用户id
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(6);
        Claims claims = TokenUtil.getClaims(token);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());

        LambdaQueryWrapper<MemberReadHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberReadHistory::getMemberId, memberId);
        lqw.eq(MemberReadHistory::getId, ids);
        remove(lqw);


    }

    @Override
    public PageDto<MemberReadHistory> pageList(HttpServletRequest request, Integer pageNum, Integer pageSize) {
        //获取用户id
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(6);
        Claims claims = TokenUtil.getClaims(token);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());

        //创建分页构造器
        Page<MemberReadHistory> memberReadHistoryPage = new Page<>(pageNum, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<MemberReadHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberReadHistory::getMemberId,memberId);

        page(memberReadHistoryPage,lqw);

        PageDto<MemberReadHistory> pageDto = new PageDto<>();
        pageDto.setPageNum(pageNum);
        pageDto.setPageSize(pageSize);
        pageDto.setTotal((int) memberReadHistoryPage.getTotal());
        pageDto.setTotalPage((int) memberReadHistoryPage.getPages());
        pageDto.setList(memberReadHistoryPage.getRecords());

        return pageDto;

    }
}
